<template>
  <el-tag :type="tagType" size="small">
    <slot>{{ displayText }}</slot>
  </el-tag>
</template>

<script setup>
import { computed } from 'vue'

const props = defineProps({
  value: {
    type: [String, Number, Boolean],
    default: null
  },
  map: {
    type: Object,
    default: () => ({})
  },
  defaultType: {
    type: String,
    default: 'info'
  }
})

const mapping = computed(() => props.map || {})

const current = computed(() => mapping.value[props.value] || mapping.value.default || {})

const displayText = computed(() => {
  if (current.value && current.value.text !== undefined) {
    return current.value.text
  }
  if (props.value === undefined || props.value === null || props.value === '') {
    return '—'
  }
  return String(props.value)
})

const tagType = computed(() => {
  if (current.value && current.value.type) {
    return current.value.type
  }
  return props.defaultType
})
</script>
